package com.cloudsigns.factorymanager2.produce.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.produce.adapter.ProduceEditProcessPlanAdapter;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartBean;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartScheduleBean;
import com.cloudsigns.factorymanager2.produce.iview.IProductOrderWorkPartScheduleEditView;
import com.cloudsigns.factorymanager2.produce.presenter.ProductOrderWorkPartScheduleEditPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.List;

public class ProduceEditProcessPlanActivity extends BaseActivity implements IProductOrderWorkPartScheduleEditView {
    private String TAG = ProduceEditProcessPlanActivity.class.getSimpleName();
    private RelativeLayout rl_back;
    private TextView tv_title;
    private ListView lv;
    private Button btn_save;

    private ProduceEditProcessPlanAdapter adapter;
    private WorkPartBean workPartBean;
    private String orderId, productId, workPartId;
    private int amount = 0;

    private ProductOrderWorkPartScheduleEditPresenter productOrderWorkPartScheduleEditPresenter;

    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_produce_edit_process_plan);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        lv = findViewById(R.id.lv);
        btn_save = findViewById(R.id.btn_save);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tv_title.setText(getResources().getString(R.string.edit_process_plan));

        Intent intent = getIntent();
        if (null != intent) {
            workPartBean = (WorkPartBean) intent.getSerializableExtra(ConstantUtil.KEY_1);
        }

        orderId = workPartBean.getOrderId();
        productId = workPartBean.getProductId();
        workPartId = workPartBean.getWorkPartId();
        amount = workPartBean.getAmount();

        adapter = new ProduceEditProcessPlanAdapter(this);
        adapter.setData(workPartBean.getWorkPartScheduleList());
        lv.setAdapter(adapter);

        productOrderWorkPartScheduleEditPresenter = new ProductOrderWorkPartScheduleEditPresenter(this);

    }

    @Override
    public void setListener() {
        rl_back.setOnClickListener(this);
        btn_save.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            // 保存
            case R.id.btn_save:
                if (workPartBean.getScheduleAmountAll() <= amount) {
                    if (!TextUtils.isEmpty(workPartId)) {
                        productOrderWorkPartScheduleEditPresenter.productOrderWorkPartScheduleEdit();
                    } else {
                        if (null != workPartBean.getWorkPartScheduleList() || !workPartBean.getWorkPartScheduleList().isEmpty()) {
                            Intent mIntent = new Intent();
                            mIntent.putExtra(ConstantUtil.KEY_1, workPartBean);
                            setResult(ConstantUtil.START_ACTIVITY_KEY_3, mIntent);
                            showToast(getResources().getString(R.string.save_success));
                            finish();
                        }
                    }
                } else {
                    showToast(getResources().getString(R.string.plan_no_greater_process));
                }
                break;
        }
    }

    //    public void saveEditData(int position, String str) {
//        workPartBean.getWorkPartScheduleList().get(position).setAmount(str);
//    }

    @Override
    public String getOrderId() {
        return orderId;
    }

    @Override
    public String getProductId() {
        return productId;
    }

    @Override
    public String getWorkPartId() {
        return workPartId;
    }

    @Override
    public List<WorkPartScheduleBean> getList() {
        return workPartBean.getWorkPartScheduleList();
    }

    @Override
    public void getSuccessed() {
        Intent mIntent = new Intent();
        mIntent.putExtra(ConstantUtil.KEY_1, workPartBean);
        setResult(ConstantUtil.START_ACTIVITY_KEY_3, mIntent);
        showToast(getResources().getString(R.string.save_success));
        finish();
    }

    @Override
    public void getFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

}